package org.anti996.lms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.anti996.lms.model.*;
import org.anti996.lms.service.DriverService;
import org.anti996.lms.service.TransportService;
import org.anti996.lms.service.TruckService;
import org.anti996.lms.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 运单 前端控制器
 * </p>
 *
 * @author The Anti996 Authors
 * @since 2022-07-15
 */
@Slf4j
@RestController
@RequestMapping("/lms/transport")
@CrossOrigin(origins = "*")
public class TransportController {
    @Autowired
    private TransportService transportService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private TruckService truckService;

//    /**
//     * 获取运单列表
//     *
//     * @return 运单列表
//     */
//    @GetMapping
//    public List<Transport> transportList() {
//        return transportService.list();
//    }

    /**
     * 获取所有运单列表
     *
     * @return 运单列表
     */
    @GetMapping
    public List<TransportOutput> transportoutputList() {
        return transportService.listAllTransports();
    }

    @GetMapping("/driver/{username}")
    public List<TransportOutput> transportOutputListByDriver(@PathVariable(name = "username") String username) {
        return transportService.listAllTransportsByDriver(username);
    }

    /**
     * 获取运单分页列表
     *
     * @param no 第no个页面
     * @param sz 每个页面的数据条数
     * @return 分页运单列表
     */
    @GetMapping("/page/{sz}/{no}")
    public List<Transport> transportListPage(@PathVariable(name = "no") int no,
                                             @PathVariable(name = "sz") int sz) {
        Page<Transport> page = transportService.page(new Page<>(no, sz));
        return page.getRecords();
    }

    /**
     * 根据运单编号获取运单
     *
     * @param t_num 运单编号
     * @return 查询到的运单
     */
    @GetMapping("/{id}")
    public Transport transport(@PathVariable(name = "id") int t_num) {
        return transportService.getById(t_num);
    }

    /**
     * 创建运单
     *
     * @param transport 运单信息
     * @return 创建结果
     */
    @PostMapping(consumes = "application/json")
    public boolean insertTransport(@RequestBody Transport transport) {
        return transportService.save(transport);
    }

    /**
     * 更新运单
     *
     * @param t_num     运单编号
     * @param transport 新的运单信息
     * @return 更新是否成功
     */
    @PutMapping(path = "/{id}", consumes = "application/json")
    public boolean updateTransport(@PathVariable(name = "id") int t_num,
                                   @RequestBody Transport transport) {
        return transportService.updateById(transport);
    }

    /**
     * 删除指定运单编号的数据
     *
     * @param t_num 运单编号
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public boolean removeTransport(@PathVariable(name = "id") int t_num) {
        return transportService.removeById(t_num);
    }

    /**
     * 根据司机编号查询司机的所有运单
     *
     * @param d_id 司机编号
     * @return 查询到的运单
     */
    @GetMapping("/transportByD/{d_id}")
    public List<Transport> transportByDriver(@PathVariable(name = "d_id") int d_id) {
        return transportService.findBydID(d_id);
    }


    @GetMapping("/getunfinish/{d_id}")//司机查询即将出发的运单
    public Transport getunfinish(@PathVariable(name = "d_id") int d_id){
        return transportService.findunfinish(d_id);
    }


    @GetMapping("/getunarrive/{d_id}")//司机到站时查询即将要修改的运单
    public Transport getunarrive(@PathVariable(name = "d_id") int d_id){
        return transportService.findunarrive(d_id);
    }
}
